package com.wave12.pro_cart.controller;

import com.github.pagehelper.PageInfo;
import com.wave12.pro_cart.entity.ProCartDetail;
import com.wave12.pro_cart.pub.ErrorCode;
import com.wave12.pro_cart.service.ProCartDetailService;
import com.wave12.utility.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: blackcat
 * @Date: 2021-06-02
 * @Description: com.wave12.pro_cart.controller
 * @version:
 */
@Controller
@RequestMapping(value = "/proCart")
public class ProCartDetailController {
    
    private static final Logger logger = LoggerFactory.getLogger(CartController.class);
    @Autowired
    private ProCartDetailService proCartDetailService;
    
    /**
     * 商家发布一个补单
     * @param request
     * @param proCartDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/createOneProCart", method = RequestMethod.POST)
    public Result createOneProCart(HttpServletRequest request, ProCartDetail proCartDetail){
        int create = proCartDetailService.createOneProCart(proCartDetail);
        if (1==create)
            return Result.succeed();
        logger.error(ErrorCode.ADD_MISS_ERROR.code().toString());
        return Result.fail(ErrorCode.ADD_MISS_ERROR.code(),"");
    }
    
    /**
     * 获取最新的补单列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getNewProCartList", method = RequestMethod.POST)
    public Result getNewProCartList(HttpServletRequest request,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer size){
        PageInfo<ProCartDetail> cartDetailList = proCartDetailService.getNewProCartList(page,size);
        if (cartDetailList.getSize()==0)
            return Result.fail(ErrorCode.SELECT_MISS_ERROR.code(),"");
        return Result.succeed(0, "查询成功", cartDetailList);
    }
    /**
     * 获取一个补单的详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getOneProCartDetail", method = RequestMethod.POST)
    public Result getOneProCartDetail(HttpServletRequest request, ProCartDetail proCartDetail){
        ProCartDetail cartDetail = proCartDetailService.getOneProCartDetail(proCartDetail);
        if (null!=cartDetail)
            return Result.succeed(0, "查询成功", cartDetail);
        return Result.fail(ErrorCode.SELECT_MISS_ERROR.code(),"");
    }
    
    /**
     * 更新一个补单的资料
     * @param request
     * @param proCartDetail
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/updateOneProCart", method = RequestMethod.POST)
    public Result updateOneProCart(HttpServletRequest request, ProCartDetail proCartDetail){
        int update = proCartDetailService.updateOneProCart(proCartDetail);
        if (1==update)
            return Result.succeed();
        logger.error(ErrorCode.UPDATE_MISS_ERROR.code().toString());
        return Result.fail(ErrorCode.UPDATE_MISS_ERROR.code(),"");
    }

}
